import 'dart:convert';

import 'package:dio/dio.dart';

class DioClient {
  final Dio _dio = Dio();

  final _baseUrl = 'http://lanhu.yx11.cn/index.php?g=portal&m=index&a=get_QiniuAuthJSON';

  final _imageUrl = 'http://portal.xumengte.com/';

  get imageUrl => _imageUrl;

// TODO: Add methods

  Future<RefererData?> getReferer() async {
    RefererData? refererData;
    try {
      Response refData = await _dio.get(_baseUrl);
      print('Referer Data: ${refData.data}');
      refererData = RefererData.fromJson(jsonDecode(refData.data));
    } on DioError catch (e) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx and is also not 304.
      if (e.response != null) {
        print('Dio error!');
        print('STATUS: ${e.response?.statusCode}');
        print('DATA: ${e.response?.data}');
        print('HEADERS: ${e.response?.headers}');
      } else {
        // Error due to setting up or sending the request
        print('Error sending request!');
        print(e.message);
      }
    }
    return refererData;
  }

}

class RefererData {
  var state;
  var info;
  var status;
  var url;
  var code;
  var referer;

  RefererData({this.state, this.info, this.status, this.url, this.code, this.referer});

  RefererData.fromJson(Map<String, dynamic> json) {
    state = json['state'];
    info = json['info'];
    status = json['status'];
    url = json['url'];
    code = json['code'];
    referer = json['referer'];
  }
}